<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class peminjaman extends CI_Controller
{
    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        
        $this->load->library('datatables');

        $this->load->model("crud/m_mohon_kredit", "", TRUE);
        $this->load->model("crud/m_angsuran", "", TRUE);
        $this->load->model("crud/m_info_usaha", "", TRUE);
        $this->load->model("crud/m_jenis_kredit", "", TRUE);
        $this->load->model("crud/m_pinjaman", "", TRUE);

    }

    function index() {
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('Form Peminjaman Kredit');
        $data["Usaha"] = $this->m_info_usaha->get("KODE_USAHA > 1");
        $data["Kredit"] = $this->m_jenis_kredit->get();
        $this->load->view('pengurus/br-03/peminjaman', $data);

//        $this->auth->is_logged_in('form/login', 2);
//        $this->base->head_html('Form Peminjaman Kredit');
//        $data["Usaha"] = $this->m_info_usaha->get("KODE_USAHA > 1");
//        $data["Kredit"] = $this->m_jenis_kredit->get();
//
//        $id_anggota = $this->session->userdata("ID_ANGGOTA");
//        $result = $this->m_pinjaman->get("pinjaman.ID_ANGGOTA = $id_anggota and pinjaman.STATUS = 0");
//        if($result->num_rows() == 0)
//            $this->load->view('pengurus/br-03/peminjaman', $data);
//        else {
//            $this->session->set_flashdata('error', "Pelayanan Baru Gagal, <br/> Pelayanan tidak bisa dilakukan karena terdapat pelayanan yang masih diproses");
//            redirect("br-03/peminjaman/list_new");
//        }
    }

    function add() {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $result = $this->m_pinjaman->insert();
        $newID = $this->db->insert_id();
        if ($newID > 0)
        //$this->generate_skema($newID);
            $this->response->status = "Insert Success";
        else
            $this->response->status = "Error Insert";

        $this->response->data = $newID;
        echo json_encode($this->response);
    }

    function get_new_bunga($jumlah, $lama, $old_bunga)
    {
        $total_bunga = "";
        $piutang = $jumlah;
        $cicilan_pokok = $jumlah / $lama;

        $total_bunga = 0;

        for ($i = 1; $i <= $lama; $i++)
        {
            $besarbunga = ($old_bunga / 100) * $piutang;
            $total_bunga += $besarbunga;
            $piutang -= $cicilan_pokok;
        }

        return $total_bunga;
    }

    function change_layanan()
    {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $this->response->data = $this->m_pinjaman->update();

        echo json_encode($this->response);
    }

    function generate_new_skema()
    {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $info_usaha = $this->input->post('JENIS_PELAYANAN', TRUE);
        $jumlah = $this->input->post('MAX_PIUTANG', TRUE);
        $lama = $this->input->post('MAX_CICIL', TRUE);
        $bunga = $this->input->post('BUNGA', TRUE);

        $jenis_kredit = $this->input->post('TIPE_KREDIT', TRUE);

        if ($info_usaha == 0 || $info_usaha == "")
        {
            $info_usaha = $this->input->post('KODE_USAHA', TRUE);
            $jenis_kredit = 2;
        }

        $tgl_pinjaman = $this->input->post("TANGGAL_PINJAM");

        $cicilanpokok = $jumlah / $lama;
        $sisahutang = $jumlah;

        if ($jenis_kredit == 2)
        {//TETAP
            if($info_usaha != 1)//JUAL BELI
                $margin = $this->input->post('MARGIN', TRUE) / $lama;
            else
                $margin = $this->get_new_bunga($jumlah, $lama, $bunga) / $lama;

            $bunga = $margin * 100 / $jumlah;

            for ($i = 1; $i <= $lama; $i++) {
                $sisahutang -= $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                'date' => $date,
                                'cicilan_ke' => $i,
                                'cicilan_pokok' => $cicilanpokok,
                                'margin' => $margin,
                                'besar_bunga' => $bunga,
                                'angsuran' => $cicilanpokok + $margin,
                                'sisa_hutang' => $sisahutang,
                );
            }
        }
        else if ($jenis_kredit == 3) {//MENURUN
            for ($i = 1; $i <= $lama; $i++) {
                $margin = ($bunga / 100) * $sisahutang;
                $sisahutang -= $cicilanpokok;
                $date = date("Y-m-d", strtotime("+$i month", strtotime($tgl_pinjaman)));
                $data[] = (object) array(
                                'date' => $date,
                                'cicilan_ke' => $i,
                                'cicilan_pokok' => $cicilanpokok,
                                'margin' => $margin,
                                'besar_bunga' => $bunga,
                                'angsuran' => $cicilanpokok + $margin,
                                'sisa_hutang' => $sisahutang,
                );
            }
        }

        $this->response->data = $data;
        echo json_encode($this->response);
    }

    function data_pinjaman_baru() {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();
        
        $result = $this->m_pinjaman->get("pinjaman.STATUS = 0");
        if($result->num_rows() > 0)
            $this->response->data = $result->result();
        else {
            $this->response->status = "EMPTY";
            $this->response->data = 0;
        }
        echo json_encode($this->response);
    }

    function notif_new() {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();
        $this->response->data = $this->m_pinjaman->get("pinjaman.STATUS = 0")->num_rows();
        echo json_encode($this->response);
    }

    function list_new() {
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('List Peminjaman Baru');
        $this->load->view('pengurus/br-03/list_peminjaman_baru');
    }

    function detail($id = null) {
        $this->auth->is_logged_in('form/login', 2);
        $this->base->head_html('Detail Peminjaman');
        
        $data["Usaha"] = $this->m_info_usaha->get("KODE_USAHA > 1");
        $data["Pinjam"] = $this->m_pinjaman->get_one($id)->first_row();

        $this->load->view('pengurus/br-03/app_peminjaman', $data);
    }

    function approve($id) {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $result = $this->m_pinjaman->approve($id);
        $this->response->status = "Peminjaman telah diterima.";
        echo json_encode($this->response);
    }

    function reject($id) {
        $this->auth->is_logged_in('form/login', 2);
        $this->response = new ajaxresponse();

        $result = $this->m_pinjaman->reject($id);
        $this->response->status = "Peminjaman telah ditolak.";
        echo json_encode($this->response);
    }

    function report($id) {
        $this->load->helper('dompdf');
        $this->load->helper('file');

        $data["Pinjam"] = $this->m_pinjaman->get_one($id)->first_row();
        $data['title'] = "Peminjaman Report";
        //$this->load->view('report/peminjaman', $data);
        $html = $this->load->view('report/peminjaman', $data, true);
        pdf_create($html, 'PJM_' . $data['Pinjam']->NAMA_ANGGOTA . '_' . $data['Pinjam']->ID_PINJAMAN);
    }

    function konsultasi($id) {
        $this->load->helper('dompdf');
        $this->load->helper('file');

        $data["Pinjam"] = $this->m_pinjaman->get_one($id)->first_row();
        $data['title'] = "Konsultasi Form";
        //$this->load->view('report/peminjaman', $data);
        $html = $this->load->view('report/konsultasi_pinjaman', $data, true);
        pdf_create($html, 'KONSUL_' . $data['Pinjam']->NAMA_ANGGOTA . '_' . $data['Pinjam']->ID_PINJAMAN);
    }
}